﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Qplus.protocol
{
    public class ProtocolUtil
    {
        /**
	    * 获取消息体长度
	    * @param buffer
	    * @return
	    */
        public static int getContentLength(byte[] buffer){
            return ((buffer[2] & 0xff) << 8) + (buffer[1] & 0xff);
        }

        /**
	    * 获取消息ID
	    * @param buffer
	    * @return
	    */
        public static int getMessageID(byte[] buffer)
        {
            return ((buffer[4] & 0xff) << 8) | (buffer[3] & 0xff);
        }

        /**
         * 生成消息头
         * @param type
         * @param len
         * @param id
         * @return
         */
        public static byte[] genMessageHead(int one, int len, int id)
        {
            byte[] heads = new byte[5];
            heads[0] = (byte)one;
            //len
            heads[1] = (byte)len;
            heads[2] = (byte)(len >> 8);

            heads[3] = (byte)id;
            heads[4] = (byte)(id >> 8);
            return heads;
        }
    }
}
